﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SCMS.CoreBusinessLogic.Web;
using SCMS.Model;
using SCMS.CoreBusinessLogic;
using SCMS.CoreBusinessLogic._Inventory;
using SCMS.UI.Models;
using SCMS.CoreBusinessLogic._GoodsReceivedNote;
using SCMS.CoreBusinessLogic.Security;
using SCMS.CoreBusinessLogic.ActionFilters;
using SCMS.CoreBusinessLogic.OrderRequest;
using SCMS.CoreBusinessLogic.WB;
using SCMS.CoreBusinessLogic.TenderAnalysis;


namespace SCMS.UI.Controllers
{
    [MyException]
    public class InventoryController : PortalBaseController
    {
        private InventoryService inventoryService;
        private IOrderRequest ORService;
        private IWayBillService wbService;
        private ITAnalysisService taService;
        public InventoryController(IPermissionService permissionService, IUserContext userContext, InventoryService _inventoryService, IOrderRequest ORService,
            IWayBillService wbService, ITAnalysisService taService)
            : base(userContext, permissionService)
        {
            this.inventoryService = _inventoryService;
            this.ORService = ORService;
            this.wbService = wbService;
            this.taService = taService;
        }

        //
        // GET: /Inventory/
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ViewGeneralInventory()
        {
            //var pdfUrl = SCMS.Reports.PdfGenerator.HtmlToPdf(pdfOutputLocation: "~/Log/",
            //   outputFilenamePrefix: "GeneratedPDF",
            //   urls: new string[] { HttpContext.Request.Url + RouteData.GetRequiredString("action") });

            List<Guid> ItemIds = new List<Guid>();
            List<Model.Inventory> inventItm = new List<Inventory>();
            foreach (var item in inventoryService.GetInventoryList(countryProg.Id))
            {
                if (ItemIds.Contains(item.ItemId)) continue;
                ItemIds.Add(item.ItemId);
                item.Quantity = inventoryService.GetItemExistingTotalQty(item.ItemId);
                inventItm.Add(item);
            }
            var model = new ViewGeneralInventoty()
            {
                EntityInventory = inventItm
            };
            return View(model);
        }

        public ActionResult GeneralInventoryPopUp(Guid itemId)
        {
            var model = new ViewGeneralInventoty()
            {
                AssetList = inventoryService.GetAssetInventoryList(countryProg.Id, null, itemId)
            };
            return View(model);
        }

        public ActionResult ViewConsumableInventory()
        {
            List<Guid> ItemIds = new List<Guid>();
            List<Model.Inventory> inventItm = new List<Inventory>();
            foreach (var item in inventoryService.GetInventoryList(countryProg.Id, null, "C"))
            {
                if (ItemIds.Contains(item.ItemId)) continue;
                ItemIds.Add(item.ItemId);
                item.Quantity = inventoryService.GetItemExistingTotalQty(item.ItemId);
                inventItm.Add(item);
            }
            var model = new ViewGeneralInventoty()
            {
                EntityInventory = inventItm
            };
            return View(model);
        }

        public ActionResult ConsumableInventoryPopUp(Guid itemId)
        {
            var model = new ViewGeneralInventoty()
            {
                EntityInventory = inventoryService.GetInventoryList(countryProg.Id, itemId).ToList()
            };
            return View(model);
        }

        public ActionResult ViewAssetInventory()
        {
            var model = new ViewAssetInventoty()
            {
                EntityAssetInventory = inventoryService.GetAssetInventoryList(countryProg.Id)
            };
            return View(model);
        }

        public ActionResult AssetInventoryPopUp(Guid id)
        {
            var model = new ViewAssetInventoty()
            {
                EntityAsset = inventoryService.GetAssetInventoryList(countryProg.Id, id).FirstOrDefault()//,
                //AssetImage = File(inventoryService.GetAssetInventoryList(id).FirstOrDefault().Image, "png")
            };
            return View(model);
        }

        public FileContentResult GetImage()
        {
            using (var db = new SCMSEntities())
            {
                //testimage prod = db.testimages.FirstOrDefault();
                Asset prod = db.Assets.FirstOrDefault(p => p.Id == new Guid("47896633-602d-4723-9fb5-a8d16f58b763"));
                if (prod != null)
                {
                    return File(prod.Image, "png");
                }
                else
                {
                    return null;
                }
            }
        }

        public ActionResult LoadEditAsset(Guid assetId)
        {
            var model = new EditAsset()
            {
                EntityAsset = inventoryService.GetAssetInventoryList(countryProg.Id, assetId).FirstOrDefault(),
                Projects = new SelectList(ORService.GetProjectNos(countryProg.Id), "Id", "ProjectNumber"),
                Warehouses = new SelectList(wbService.GetWarehouses(countryProg.Id), "Id", "Name"),
                Staffs = new SelectList(taService.GetStaffs(countryProg.Id), "StaffID", "StaffName")
            };
            return View(model);
        }

        public ActionResult EditAssetDetails(EditAsset assetModel)
        {
            inventoryService.IsAssetEdited(assetModel.EntityAsset);
            var model = new ViewAssetInventoty()
            {
                EntityAsset = inventoryService.GetAssetInventoryList(countryProg.Id, assetModel.EntityAsset.Id).FirstOrDefault()
            };
            return View("AssetInventoryPopUp", model);
        }

        public ActionResult LoadAssetDepreciation()
        {
            var model = new AssetDepreciation()
            {
                EntityAsset = new Asset(),
                Assets = new SelectList(inventoryService.GetAssetList(countryProg.Id), "Id", "Name")
            };
            return View(model);
        }

        public ActionResult ViewDeprSummary(Guid id)
        {
            var model = new ViewAnnualDepreciation() { DeprEntity = inventoryService.GetAnnualDepreciation(id) };
            return View(model);
        }

        public ActionResult ViewDetailDepr(Guid id)
        {
            var model = new ViewAnnualDepreciation() { DeprEntity = inventoryService.GetDetailedDepreciation(id) };
            return View(model);
        }
    }
}
